package game.module.ddz.processor;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.entity.PlayingRole;
import game.module.ddz.bean.DdzRound;
import game.module.ddz.logic.DdzDataManager;
import game.module.init.logic.RoomConnectManager;
import game.module.user.dao.LanSessionCache;
import game.session.SessionManager;
import lion.common.ArrayUtility;
import lion.common.MsgCodeAnn;
import lion.netty4.message.GamePlayer;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lion.netty4.processor.MsgProcessor;
import lua.LuaMessageDdz.C2SDdzConnectRoom;
import lua.LuaMessageDdz.PushDdzConnectRoom;
import lua.LuaMessageDdz.S2CDdzConnectRoom;

@MsgCodeAnn(msgcode = C2SDdzConnectRoom.id, accessLimit = 500)
public class DdzRoomConnectProcessor extends MsgProcessor {

	private static Logger logger = LoggerFactory.getLogger(DdzRoomConnectProcessor.class);

	@Override
	public void process(GamePlayer gamePlayer, RequestMessageRaw request) throws Exception {
		C2SDdzConnectRoom reqMsg = C2SDdzConnectRoom.parse(request);
		long roomSessionId = reqMsg.sessionId;
		logger.info("ddz room connect,sessionId={}", roomSessionId);
		// 判断session是否有效
		Integer playerId = LanSessionCache.getInstance().getCachePlayerId(roomSessionId);
		if (playerId == null) {
			S2CDdzConnectRoom retMsg = new S2CDdzConnectRoom();
			retMsg.is_success = 1;
			gamePlayer.writeAndFlush(retMsg.build(gamePlayer.alloc()));
			return;
		}
		// 创建玩家
		RoomConnectManager.cacheNewPlayer(roomSessionId, gamePlayer, playerId);
		// ddz房间是否存在
		DdzRound ddzRound = DdzDataManager.getInstance().getDdzRoom(playerId);
		if (ddzRound == null) {
			S2CDdzConnectRoom retMsg = new S2CDdzConnectRoom();
			retMsg.is_success = 2;
			gamePlayer.writeAndFlush(retMsg.build(gamePlayer.alloc()));
			return;
		}
		byte[] connectedPlayerIndexs = null;
		synchronized (ddzRound) {
			Set<Integer> connectedPlayerSet = ddzRound.getConnectedPlayerIndexSet();
			if(connectedPlayerSet == null) {
				connectedPlayerSet = new HashSet<>();
				ddzRound.setConnectedPlayerIndexSet(connectedPlayerSet);
			}
			int myIndex = ddzRound.getPlayerIds().indexOf(playerId);
			connectedPlayerSet.add(myIndex);
			connectedPlayerIndexs = ArrayUtility.toByteArray(connectedPlayerSet);
		}
		// ret
		S2CDdzConnectRoom retMsg = new S2CDdzConnectRoom();
		retMsg.is_success = 0;
		retMsg.connected_player_indexs = connectedPlayerIndexs;
		gamePlayer.writeAndFlush(retMsg.build(gamePlayer.alloc()));
		//push
		int myIndex = ddzRound.getPlayerIds().indexOf(playerId);
		PushDdzConnectRoom pushMsg = new PushDdzConnectRoom((byte) myIndex);
		List<Integer> playerIds = ddzRound.getPlayerIds();
		for (Integer aPlayerId : playerIds) {
			PlayingRole pr = SessionManager.getInstance().getPlayer(aPlayerId);
			if (pr != null && pr.isChannelActive()) {
				pr.writeAndFlush(pushMsg.build(pr.alloc()));
			}
		}
	}

	@Override
	public void process(GamePlayer player, RequestProtoMessage request) throws Exception {
		// null
	}

	@Override
	public void process(GamePlayer player, MyRequestMessage request) throws Exception {
		// null
	}

}
